import random
import numpy as np
import pandas as pd
import networkx as nx
import torch

from network_topology import GBN, GEANT2, weight_update, draw_graph

# 可以使用pandas构件图
edge = pd.DataFrame()
edge['source'] = [1, 1, 2, 3]
edge['target'] = [2, 3, 3, 4]
edge['weight'] = [10, 9, 8, 7]

g_test, e_test = GEANT2()
print(g_test.nodes.__len__())
print(g_test.edges.__len__())

# Graph_1 = nx.from_pandas_edgelist(edge, source='sources', target='targets', edge_attr='weights')
Graph_1, edge = GBN()

'''
# 一些可能会用到的，图的一些特征属性
print(nx.degree(Graph_1))
# 图的直径长度
print(nx.diameter(Graph_1))
# 度中心性
print(nx.degree_centrality(Graph_1))
# 特征向量中心性
print(nx.eigenvector_centrality(Graph_1))
# betweeness
print(nx.betweenness_centrality(Graph_1))
'''

# 图的nodes和edge属性，都是list
print(torch.cuda.current_device())
# 节点的个数
print(Graph_1.nodes.__len__())
# 边的个数
print(Graph_1.edges.__len__())

# 打印整个图
print(Graph_1.edges(data=False))

# 打印一个边的权重
print(Graph_1[0][2]['weight'])

edge_labels = nx.get_edge_attributes(Graph_1, 'weight')
print(edge_labels.keys())
print("所有边的权重")
print(list(edge_labels.values()))

# 读每个边的情况
# for item in Graph_1.edges:
#     print(item[0], item[1])

# 更新图的边权值
new_weight = np.empty(26)
for i in range(0, 26):
    new_weight[i] = random.randint(50, 100)
new_weight = torch.tensor(new_weight, dtype=torch.float32)
# G = weight_update(Graph_1, new_weight)
print(Graph_1[0][2]['weight'])

draw_graph(Graph_1)

# 这里返回的是一个生成器类，共有
# print(type(nx.all_pairs_dijkstra_path(G, new_weight)))
path_generator = nx.all_pairs_dijkstra_path(Graph_1)
short_paths = []
for item in path_generator:
    short_paths.append(item)
short_paths = sorted(short_paths)
print(short_paths)

'''
尝试弗洛伊德算法
'''
print('floyd_warshall_alg')
floyd_ = []
path_generator = nx.floyd_warshall_numpy(Graph_1, weight='weight')
for item in path_generator:
    floyd_.append(item)
print(floyd_)

# 对迪杰斯特拉算法进行测试，确认是依据权重计算的
# 修改了一些特定权重，确认是根据这个计算的
# G[0][2]['weight'] = 1
# G[2][1]['weight'] = 1
# G[2][4]['weight'] = 0.1
# G[4][1]['weight'] = 0.1
#
# draw_graph(G)
#
# new_generator = nx.all_pairs_dijkstra_path(G)
# print(next(new_generator))
# print(next(new_generator))

'''
以下是使用plt绘制图的办法，包括每个点的编号以及边的权重

pos = nx.spring_layout(Graph_1)
nx.draw(Graph_1, pos, with_labels=True, font_weight='bold')
edge_labels = nx.get_edge_attributes(Graph_1, 'weight')
nx.draw_networkx_edge_labels(Graph_1, pos, edge_labels=edge_labels)

plt.show()
print(nx.adjacency_matrix(Graph_1).todense())
'''

# matplotlib&networkx导入与图绘制测试
# G = nx.petersen_graph()
# subax1 = plt.subplot(121)
# nx.draw(G, with_labels=True, font_weight='bold')
# subax2 = plt.subplot(122)
# nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')
# plt.show()
